package com.example.controller;
import com.example.dao.BlogMapper;
import com.example.model.Blog;
import com.example.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blog")
public class BlogController {


    @GetMapping
    public List<Blog> getBlog() {
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        List<Blog> blogs = mapper.selectBlog();
        for (Blog blog : blogs) {
            System.out.println("blog:" + blog);
        }
        session.close();
        return blogs;
    }

    @GetMapping("/id/{id}")
    public List<Blog> getBlogByID(@PathVariable String id) {
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        List<Blog> blogs = mapper.selectBlogByID(Integer.parseInt(id));
        for (Blog blog : blogs) {
            System.out.println("blog:" + blog);
        }
        session.close();
        return blogs;
    }

    @GetMapping("/title")
    public List<Blog> getBlogByTitle(@RequestParam("title") String title) {
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);

        Map<String, Object> map = new HashMap<>();
        map.put("title", title);

        List<Blog> blogs = mapper.selectBlogByTitle(map);
        for (Blog blog : blogs) {
            System.out.println("blog:" + blog);
        }
        session.close();
        return blogs;
    }

    @PostMapping
    public String addBlog(@RequestBody Blog blog) {
        System.out.println(blog);
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        int result = mapper.addBlog(blog);
        session.commit();
        session.close();
        if (result > 0) {
            return "add success";
        } else {
            return "add failed";
        }
    }

    @PutMapping
    public String updateBlog(@RequestBody Blog blog) {
        System.out.println(blog);
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        int result = mapper.updateBlog(blog);
        session.commit();
        session.close();
        if (result > 0) {
            return "update success";
        } else {
            return "update failed";
        }
    }

    @DeleteMapping
    public String deleteBlog(@RequestParam("id") Integer id) {
        SqlSession session = MybatisUtils.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        int result = mapper.deleteBlog(id);
        session.commit();
        session.close();
        if (result > 0) {
            return "delete success";
        } else {
            return "delete failed";
        }
    }


}
